package com.nxy.cpfsfe.service.factory;

import com.adtec.starring.exception.BaseException;
import com.adtec.starring.global.SysDef;
import com.adtec.starring.respool.SystemParameter;
import com.nxy.cpfsfe.enumtype.MsgCdOrTpEnum;
import com.nxy.cpfsfe.enumtype.RespCd;
import com.nxy.cpfsfe.util.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 借记卡信息查询申请处理
 * Created by wyj on 2019/12/26.
 */
public class DebitCrdInfQryServer implements ForwardServer{
    private static DebitCrdInfQryServer server = new DebitCrdInfQryServer();
    private DebitCrdInfQryServer(){}
    public static DebitCrdInfQryServer getInstance(){
        return server;
    }
    private final static Logger LOGGER = LoggerFactory.getLogger(DebitCrdInfQryServer.class);

    @Override
    public String buildEsbXml(Map<String, String> map) {
        LOGGER.info("---------借记卡信息查询申请报文CBS0000030--------");
        try {
            XMLNodeUtil xmlReqDoc = new XMLNodeUtil(XMLNodeUtil.FROM_FILE,
                    SysDef.WORK_DIR + SystemParameter.SRC_PATH + SysDef.FILE_SP + "xml" + SysDef.FILE_SP + "CBS" + SysDef.FILE_SP + "CBS_0000030.xml");
            //报文头
            XmlCBSBuildUtil.buildXmlRequestHead(xmlReqDoc, MsgCdOrTpEnum.CBS_0000030.getCode(), map);
            //报文体
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/acct ",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/AcctNo")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/chkPwdFlg",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/AcctPwdFlg")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/pwdTyp",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/AcctPwdTp")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/pwd",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/AcctNoFlg")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/subAcct",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/AcctSubNo")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/currencyTyp",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/TrxCurTpCd")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/cashFlg",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/TrxCashTp")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/acctFlg",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/OnLineBkHangFlg")));
            return xmlReqDoc.toString();
        } catch (Exception e) {
            LOGGER.error("借记卡信息查询申请报文CBS0000030失败", e);
            throw new BaseException(RespCd.NCPS2402, e, "借记卡信息查询申请报文CBS0000030失败");
        }
    }

    @Override
    public String handleRpsMsg(Map<String, String> map, String respMsg) {
        LOGGER.info("---------借记卡信息查询回执报文CBS0000031--------");
        try {
            XMLNodeUtil xmlReqDoc = new XMLNodeUtil(XMLNodeUtil.FROM_FILE,
                    SysDef.WORK_DIR + SystemParameter.SRC_PATH + SysDef.FILE_SP + "xml" + SysDef.FILE_SP + "CPFS" + SysDef.FILE_SP + "CPFS_305.xml");
            //报文头
            XmlCPFSBuildUtil.buildXmlResponseHead(xmlReqDoc, MsgCdOrTpEnum.MSGTD_305.getCode(), map);
            //解析cbs0000031报文
            if(StringUtils.isNotBlank(respMsg)){
                Map<String,String> rpsmap = XmlUtil.xml2Map(respMsg);
                String  retCd = StringUtil.trimObject(rpsmap.get("/transaction/header/status/retCd"));
                //返回码前两位为00代表成功
                if(retCd.startsWith("00")&&retCd.length() ==6){
                    map.put("msg", StringUtil.trimObject(rpsmap.get("/transaction/header/status/desc")));
                    map.put("resultCode",retCd);
                    map.put("custID",StringUtil.trimObject(rpsmap.get("/transaction/body/response/custID")));
                    map.put("custNm",StringUtil.trimObject(rpsmap.get("/transaction/body/response/custNm")));
                    map.put("openBrchID",StringUtil.trimObject(rpsmap.get("/transaction/body/response/openBrchID")));
                    map.put("openBrchNm",StringUtil.trimObject(rpsmap.get("/transaction/body/response/openBrchNm")));
                    map.put("openBrchNmSt",StringUtil.trimObject(rpsmap.get("/transaction/body/response/openBrchNmSt")));
                    map.put("acct",StringUtil.trimObject(rpsmap.get("/transaction/body/response/acct")));
                    map.put("subAcct",StringUtil.trimObject(rpsmap.get("/transaction/body/response/subAcct")));
                    map.put("cardNb",StringUtil.trimObject(rpsmap.get("/transaction/body/response/cardNb")));
                    map.put("openDt",StringUtil.trimObject(rpsmap.get("/transaction/body/response/openDt")));
                    map.put("voucherTyp",StringUtil.trimObject(rpsmap.get("/transaction/body/response/voucherTyp")));
                    map.put("acctTyp",StringUtil.trimObject(rpsmap.get("/transaction/body/response/acctTyp")));
                    map.put("acctStat",StringUtil.trimObject(rpsmap.get("/transaction/body/response/acctStat")));
                    map.put("acctKind",StringUtil.trimObject(rpsmap.get("/transaction/body/response/acctKind")));
                    map.put("currencyTyp",StringUtil.trimObject(rpsmap.get("/transaction/body/response/currencyTyp")));
                    map.put("cashFlg",StringUtil.trimObject(rpsmap.get("/transaction/body/response/cashFlg")));
                    map.put("bal",StringUtil.trimObject(rpsmap.get("/transaction/body/response/bal")));
                    map.put("avlBal",StringUtil.trimObject(rpsmap.get("/transaction/body/response/avlBal")));
                    map.put("intrstBgnDt",StringUtil.trimObject(rpsmap.get("/transaction/body/response/intrstBgnDt")));
                    map.put("savePeriod",StringUtil.trimObject(rpsmap.get("/transaction/body/response/savePeriod")));
                    map.put("dueDt",StringUtil.trimObject(rpsmap.get("/transaction/body/response/dueDt")));
                    map.put("intrstRate",StringUtil.trimObject(rpsmap.get("/transaction/body/response/intrstRate")));
                }else{
                    map.put("msg", StringUtil.trimObject(rpsmap.get("/transaction/header/status/desc")));
                    map.put("resultCode",retCd);
                }
            }else{
                LOGGER.error("cbs0000031回执报文为空，请联系");
                map.put("msg", "cbs0000031回执报文为空，请联系");
                map.put("resultCode",RespCd.NCPO0005);
            }
            //报文体
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/BizInf/TrxStsCd",  StringUtil.trimObject(map.get("resultCode")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/BizInf/BizStsCd",  StringUtil.trimObject(map.get("resultCode")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/BizInf/BizStsDesc",  StringUtil.trimObject(map.get("msg")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/custID",  StringUtil.trimObject(map.get("custID")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/custNm",  StringUtil.trimObject(map.get("custNm")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctNo",  StringUtil.trimObject(map.get("acct")));
            // 这里不确定
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctNm",  StringUtil.trimObject(map.get("custNm")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctSubNo",  StringUtil.trimObject(map.get("subAcct")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/cardNo",  StringUtil.trimObject(map.get("cardNb")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/BrnchId",  StringUtil.trimObject(map.get("openBrchID")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/BrnchNm",  StringUtil.trimObject(map.get("openBrchNm")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/DpsDt",  StringUtil.trimObject(map.get("openDt")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctVchrTp",  StringUtil.trimObject(map.get("voucherTyp")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctAgTp",  StringUtil.trimObject(map.get("acctTyp")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctSts",  StringUtil.trimObject(map.get("acctStat")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctAgProp",  StringUtil.trimObject(map.get("acctKind")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctCurTpCd",  StringUtil.trimObject(map.get("currencyTyp")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctCashTp",  StringUtil.trimObject(map.get("cashFlg")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctBalAmt",  StringUtil.trimObject(map.get("bal")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctAvBalAmt",  StringUtil.trimObject(map.get("avlBal")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/BegIntDt",  StringUtil.trimObject(map.get("intrstBgnDt")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/DepCyc",  StringUtil.trimObject(map.get("savePeriod")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/DueDt",  StringUtil.trimObject(map.get("dueDt")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/IntrstRate",  StringUtil.trimObject(map.get("intrstRate")));
            return xmlReqDoc.toString();
        } catch (Exception e) {
            LOGGER.error("借记卡信息查询回执报文CBS0000031失败", e);
            throw new BaseException(RespCd.NCPS2402, e, "借记卡信息查询回执报文CBS0000031失败");
        }
    }
}
